<?php
/**
 *  
 * 
 * @package Cms
 * @license 2008 
 */

/**
 * Collabor 
 *
 * @package Cms
 * 
 */
class Cms_BreadCrumb
{
	public $_namespace = 'bread_crumb';
	private $_crumbs = null;
	
	public function __construct() 
	{
		
	}
	
	public function setNameSpace($value)
	{
		$this->_namespace = $value;
	}
	
	public function setCrumbs($value)
	{
		$this->_crumbs = $value;	
	}
	
	public function addBreadCrumb($value = 'default',$url = '')
	{
		$value = $value  !=''?$value:'default';
		$crumb = null;
		$crumb['request_url'] = $url == $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI']  : $url;
		$crumb['crumb_name'] = $value;
		
		$_SESSION[$this->_namespace][] = $crumb;
	}
	
	public function reduceBreadCrumb()
	{
		if(is_array($_SESSION[$this->_namespace])) {
			$breadCrumb = null;
			
			if($this->_crumbs == null  ) {
				foreach($_SESSION[$this->_namespace] as $crumb)
				{
					if($crumb['request_url'] == $_SERVER['REQUEST_URI']) {
						break;
					}
					$breadCrumb[] = $crumb;
				}
				
			} else {
				for($i = 1;$i <= $this->_crumbs;$i++)
				{
					if($_SESSION[$this->_namespace][$i -1]['request_url'] == $_SERVER['REQUEST_URI']) {
						break;
					}
					
					if(empty($_SESSION[$this->_namespace][$i -1]['request_url'])) {
						break;
					} else { 
						$breadCrumb[] = $_SESSION[$this->_namespace][$i -1];
					}
				}
			}
			$_SESSION[$this->_namespace] = $breadCrumb;
		}
		
	}

	
	public function displayBreadCrumb()
	{
		$breadCrumb = null;
		if(isset($_SESSION[$this->_namespace])) {
			foreach($_SESSION[$this->_namespace] as $crumb)
			{
				if($breadCrumb == null) {
					$breadCrumb = '<a href="'.$crumb['request_url'].'" class="bodytext10pt">'.$crumb['crumb_name'].'</a>';
				} else {
					$breadCrumb .= ' &raquo; <a href="'.$crumb['request_url'].'" class="bodytext10pt">'.$crumb['crumb_name'].'</a>';
				}
			}
		}
		return $breadCrumb;
	}
	
	public function clear()
	{
		unset($_SESSION[$this->_namespace]);
	}
}